﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using Legazpi.Payroll.DataAccess.Base;
using Legazpi.Payroll.DataAccess.Repositories;

namespace Legazpi.Payroll.BusinessLogic.Services
{
    public class EmployeeService : IEmployeeService
    {

        #region Fields

        private DbContext _dbContext;
        private IEmployeeRepository _employeeRepository;        
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public EmployeeService()
            : this(new DatabaseFactory())
        {

        }

        public EmployeeService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _employeeRepository = new EmployeeRepository(_dbContext);                
            }
        }

        #endregion

        public Employee GetById(int id)
        {
            return _employeeRepository.GetById(id);
        }

        public IEnumerable<Employee> GetAll()
        {
            return _employeeRepository.GetAll();
        }

        public IEnumerable<Employee> Get(int multiplier, int count)
        {
            return _employeeRepository.GetQuery()
            .OrderBy(x => x.LastName)
            .Skip(multiplier * count)
            .Take(count)
            .ToList();
        }

        public void Add(Employee employee)
        {
            employee.EmployeeCode = GetNewEmployeeCode();
            employee.DateAdded = DateTime.Now;
            _employeeRepository.Add(employee);
        }

        public void Update(Employee employee)
        {
            if (string.IsNullOrEmpty(employee.EmployeeCode))
                employee.EmployeeCode = GetNewEmployeeCode();
            employee.DateModified = DateTime.Now;
            _employeeRepository.Update(employee);
        }

        public void Delete(int id)
        {
            var employee = GetById(id);
            if (employee != null)
                _employeeRepository.Delete(employee);
        }

        public bool Save()
        {
            string message;
            _employeeRepository.Save(out message);
            return message == "Saved";
        }


        public IEnumerable<Employee> Get()
        {
            throw new NotImplementedException();
        }

        #region  Methods
        private string GetNewEmployeeCode()
        {
            //var employeeCodeSetting = _settingsService.GetBySettingName("EMPLOYEECODENO");
            //int employeeCodeno;
            //int.TryParse(employeeCodeSetting.SettingValue, out employeeCodeno);
            //string employeeCode = _settingsService.GetValueBySettingName("EMPLOYEECODE");           
            //string newEmployeeCodeNo;
            
            //employeeCodeno = employeeCodeno + 1;
            //employeeCodeSetting.SettingValue = employeeCodeno.ToString();
            //_settingsService.Update(employeeCodeSetting); _settingsService.Save();            
            //newEmployeeCodeNo = string.Format("{0}-{1}", employeeCode, employeeCodeno.ToString().PadLeft(5, '0'));

            return "Emp-001";
        }
        #endregion
    }
}
